package ht_birds;

import hypertree.HyperTree;
import hypertree.SwingHTView;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;

import ui.mainWindow;

import dao.BirdDAO;
import dao.BirdVO;
import dao.ConnectionManager;

/**
 * The Demo class implements a demo for HyperTree. It display an HyperTree view
 * of a demo tree. The demo tree is build from a file tree, passed in parameter.
 * Demo could take an argument, the path from which start the representing of
 * files. If no arguments is given, the treemap start from the root.
 */
public class hyper_birdsROE {

	private static HTDBNode root = null; // the root of the demo tree
	private static HyperTree hypertree = null; // the hypertree builded

	/**
	 * Display a demo HyperTree.
	 * 
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		String pathRoot = null;

		if (args.length > 0) {
			pathRoot = args[0];
		} else {
			pathRoot = "birds";
		}

	//	try {
	//		ConnectionManager.connect(mainWindow.BD_NAME);
	//	} catch (SQLException ex) {
	//		System.out.println("Error while connecting to db: " + ex);
	//	} catch (ClassNotFoundException ex) {
	//		System.out.println("Class not found, check classpath");
	//	}

		
		//TODO nao retorna os valores corretamente fiz oq ele deveria retornar
		// Vector<BirdVO> bv = BirdDAO.arbitrarySQL("select risk_of_extinction from bird");
		Vector<String>bv = new Vector<String>();
	/*	bv.add(new BirdVO (null, "Least Concern", null, 0, null, 0, 0, null, 0, 0, null));
		bv.add(new BirdVO (null, "Near Threatened", null, 0, null, 0, 0, null, 0, 0, null));
		bv.add(new BirdVO (null, "Vulnerable", null, 0, null, 0, 0, null, 0, 0, null));*/
		
		root = new HTDBNode("birds");
		
		PreparedStatement comandoSelect = ConnectionManager.getConnection().prepareStatement("SELECT DISTINCT risk_of_extinction FROM bird");
		ResultSet resultados = comandoSelect.executeQuery();
		while(resultados.next())
		{
		
			String roe = resultados.getString(1);
			
			Vector<BirdVO> bv_roe = BirdDAO.arbitrarySQL("select * from bird where risk_of_extinction = \'"+ roe + "\'");
			System.out.println("---"+roe +": " + bv_roe.size());
			
				if (bv_roe.size() > 0) {
				
				HTDBNode child = new HTDBNode(roe);
				
				for (int i = 0; i < bv_roe.size(); i++) {

					HTDBNode grandson = new HTDBNode(bv_roe.get(i));
					child.addChild(grandson);
					System.out.println(bv_roe.get(i).getScientific_name());
				}
				
				root.addChild(child);

			}

			
		}

		
		
	//	try {
	//		ConnectionManager.disconnect();
	//	} catch (SQLException e) {
	//		e.printStackTrace();
	//	}
//
		hypertree = new HyperTree(root);
		SwingHTView view = hypertree.getView();
		// HTNode firstchild = (HTNode) root.children().next();
		// view.translateToOrigin(firstchild);
		JFrame viewFrame = new JFrame("Birds HyperTree");
		viewFrame.setContentPane(view);
		viewFrame.setSize(500, 500);
		// viewFrame.pack();
		viewFrame.setVisible(true);

	}

}
